package com.gmall.rabbitmq.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class HeaderExchange_Queue_Config {

    public final static String HEADER_EXCHANGE = "header_exchange";
    // 声明队列
    public final static String HEADER_EX_QUEUE_1 = "header_queue_1";

    public final static String HEADER_EX_QUEUE_2 = "header_queue_2";



    // 声明交换机
    @Bean("headerExchange")
    public Exchange headerExchange(){
        //Builder 设计模式
        return ExchangeBuilder.headersExchange(HEADER_EXCHANGE)
                .durable(true)//默认RabbitMQ 重启以后， 交换机就删掉，除非是durable=true
                .build();
    }

    // 声明队列
    @Bean("headerQueue1")
    public Queue headerQueue1(){
        return QueueBuilder.durable(HEADER_EX_QUEUE_1).build();
    }
    @Bean("headerQueue2")
    public Queue headerQueue2(){
        return QueueBuilder.durable(HEADER_EX_QUEUE_2).build();
    }

    @Bean("headerBinding1")
    public Binding headerBinding1(
            @Qualifier("headerQueue1") Queue queue,
            @Qualifier("headerExchange") HeadersExchange exchange){
        Map map = new HashMap<>();
        map.put("Inform1","email");
        map.put("Inform2","sms");
        return BindingBuilder.bind(queue).to(exchange).whereAll(map).match();
    }

    @Bean("headerBinding2")
    public Binding headerBinding2(
            @Qualifier("headerQueue2") Queue queue,
            @Qualifier("headerExchange") HeadersExchange exchange){
        Map map = new HashMap<>();
        map.put("Inform1","email");
        map.put("Inform2","qq");
        return BindingBuilder.bind(queue).to(exchange).whereAny(map).match();
    }


}
